package com.xiexiaozhou.helper.core.service.impl;

import com.xiexiaozhou.helper.core.domain.Customer;
import com.xiexiaozhou.helper.core.repository.CustomerRepository;
import com.xiexiaozhou.helper.core.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    /**
     * 顾客注册
     *
     * @param customer
     * @return
     */
    @Override
    public Customer register(Customer customer) {
        customer.setApplyTime(new Date());
        return customerRepository.save(customer);
    }

    /**
     * 顾客登录
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public Customer login(String username, String password) {
        return customerRepository.findByUsernameAndPassword(username, password);
    }

    /**
     * 顾客列表（分页）
     *
     * @param pageable
     * @return
     */
    @Override
    public Page<Customer> list(Pageable pageable) {
        return customerRepository.findAll(pageable);
    }

    /**
     * 获取所有配送员
     *
     * @return
     */
    @Override
    public List<Customer> listAllDistributor() {
        return customerRepository.findByUserType(2);
    }

    /**
     * 通过顾客id获取顾客信息
     *
     * @param customerId
     * @return
     */
    @Override
    public Customer get(Long customerId) {
        return customerRepository.findOne(customerId);
    }

    /**
     * 修改顾客信息
     *
     * @param customer
     * @return
     */
    @Override
    public Customer update(Customer customer) {
        return customerRepository.save(customer);
    }

    /**
     * 申请配送资格,申请状态改为1:申请中
     *
     * @param customerId
     * @return
     */
    @Override
    public Customer applyToDistributor(Long customerId) {
        //通过id获取顾客
        Customer customer = customerRepository.findOne(customerId);

        //申请状态
        customer.setApplyStatus(1);
        //申请时间
        customer.setApplyTime(new Date());

        //更新顾客信息
        return customerRepository.save(customer);
    }

    /**
     * 审核配送资格
     *
     * @param customerId
     * @param applyStatus
     * @return
     * @throws Exception
     */
    @Override
    public Customer confirmToDistributor(Long customerId, Integer applyStatus) throws Exception {
        //通过id获取顾客信息
        Customer customer = customerRepository.findOne(customerId);
        //修改申请状态
        if (applyStatus == 2) {
            customer.setApplyStatus(applyStatus);
            customer.setUserType(2);
        } else if (applyStatus == 2) {
            customer.setApplyStatus(applyStatus);
        } else {
            throw new Exception("参数错误");
        }
        return customerRepository.save(customer);
    }
}
